package com.os.opencv.java.chapter4;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;

public class Mask {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        //读取图像文件并在屏幕上显示
        Mat src = Imgcodecs.imread("/Users/matt/MATT-MAC/matt_workspace/OS-OpenCV_workspace/view.jpg");
        HighGui.imshow("src", src);
        HighGui.waitKey(0);

        //创建掩膜图像
        Mat mask;
        Scalar black = new Scalar(0,0,0);
        Scalar white = new Scalar(255,255,255);
        mask = new Mat(src.size(), src.type(), black);

        //定义掩膜区域（矩形区域、白色）
        Rect roi = new Rect(400,400,400,200);
        Mat sub = mask.submat(roi);
        sub.setTo(white);

        //在屏幕上显示掩膜图像
        HighGui.imshow("mask", mask);
        HighGui.waitKey(0);

        //生成带有掩膜的图像并在屏幕上显示
        Mat dst = new Mat(src.size(), src.type(), black);
        src.copyTo(dst, mask);
        HighGui.imshow("dst", dst);
        HighGui.waitKey(0);

        System.exit(0);

    }
}
